package com.yanqu.road.server.cmd.activity.union;

import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import java.util.Map;

@Cmd(code = CrossProtocol.C3_CHANNEL_ACTIVITY_GET_UNION_REQ, desc = "")
public class Cross3UpdateUnionReqCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ChannelTonBingProto.ChannelGetUnionUpdateReqMsg reqMsg = ChannelTonBingProto.ChannelGetUnionUpdateReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        long gameServerId = reqMsg.getGameServerId();
        ChannelTonBingProto.ChannelGetUnionUpdateRespMsg.Builder builder = ChannelTonBingProto.ChannelGetUnionUpdateRespMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setGameServerId(gameServerId);
        for(String unionId : reqMsg.getUnionUidList()){
            CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionId);
            Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(unionId);
            if (unionInfo != null && unionMemberMap != null) {
                ChannelTonBingProto.CUnionMsg.Builder tmp = ChannelTonBingProto.CUnionMsg.newBuilder();
                tmp.setUnionUid(unionId);
                UnionProto.UnionBaseTempMsg.Builder gaga = UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo));
                for (CrossUnionMember member : unionMemberMap.values()) {
                    if(member.getPosition() == eUnionPosition.Master.getValue()){
                        if(!unionInfo.getMasterNickName().equals(member.getUserBaseInfo().getNickName()) && gaga != null){
                            gaga.setMasterNickName(member.getUserBaseInfo().getNickName());
                        }
                    }
                    tmp.addMember(ChannelTonBingProto.CUnionMemberInfoMsg.newBuilder()
                            .setUserId(member.getUserId())
                            .setServerId(member.getServerId())
                            .setPosition(member.getPosition())
                            .setChannelId(ServerListMgr.getChannelId(member.getServerId()))
                    );
                }
                tmp.setUnionInfo(gaga);
                builder.addUnion(tmp);
            }
        }

        MessageHelper.sendPacket(serverId,0,YanQuMessageUtils.buildMessage(CrossProtocol.C2_CHANNEL_ACTIVITY_GET_UNION_REQ_BACK, builder));
    }

    @Override
    public void sendData() {

    }

}
